﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace OINS_VS08
{
    class Server
    {
        IPAddress localAddr ;
        TcpListener tcpListener; 
        public Server(String ip, int port)
        {
            localAddr = IPAddress.Parse(ip);
            tcpListener = new TcpListener(localAddr, port);

        }
        public void Run()
        {
            System.Console.WriteLine("server is running");
            tcpListener.Start();
            for( ; ;)
            {
                Socket socketForClient=tcpListener.AcceptSocket();
                Console.WriteLine("Klient zostal podlaczony");
                //SendFileToClient(socketForClient,"d:/a.txt");
                Console.WriteLine("Zrywanie polaczenia");
                socketForClient.Close();
                Console.WriteLine("Koniec..");
                //break;
            }
        }
        private void SendFileToClient( Socket socketForClient, String file)
        {
            NetworkStream networkStream= new NetworkStream(socketForClient);
            System.IO.StreamWriter streamWriter= new System.IO.StreamWriter(networkStream);
            System.IO.StreamReader streamReader=new System.IO.StreamReader(@"d:\a.txt");
            string theString;

            do
            {
                theString=streamReader.ReadLine();
                if(theString !=null)
                {    
                    Console.WriteLine("wysylanie: {0}", theString);
                    streamWriter.WriteLine(theString);
                    streamWriter.Flush();
                }
            }
            while(theString!=null);
            streamReader.Close();
            networkStream.Close();
            streamWriter.Close();
        }
    }
}



        